<?php

class Bid_ProductController extends My_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        parent::init();
        $this->_helper->_layout->setLayout('2columns-left');
    }

    public function create1Action()
    {
    	//Create product in database
    	$ss = new Zend_Session_Namespace("product");
    	if(empty($ss->id))
    	{
			$model = $this->loadModel('bid','Product');
			$newrow = $model->createrow();
			$newrow->save();
			$ss->id = $newrow->id;
		}
		
    	//=== end create product ===
    	
    	$model = $this->loadModel('bid','Category');
    	$select = $model->select()->where('parent_id=?',1);
		$categories = $model->fetchAll($select);
		$this->view->categories = $categories;
		
		$select = $model->select()->from($model, array(new Zend_Db_Expr('max(level) as maxlevel')));
		$rowSet = $model->fetchRow($select);
		$maxLevel = $rowSet->maxlevel;
		$this->view->max_level = $maxLevel;
    }
    
    public function create2Action()
    {
    	$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);
			$product = $model->fetchRow($select);
			$this->view->product = $product;
		}
    }
    
    public function create3Action()
    {
    	$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);
			$product = $model->fetchRow($select);
			$this->view->product = $product;
		}
    }
    
    public function create4Action()
    {
    	$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);

			$product = $model->fetchRow($select);
			$this->view->product = $product;
			
			if($product->cat_id > 0)
			{
				$model = $this->loadModel('bid','Category');
				$select = $model->select()->where('id=?',$product->cat_id);
				$this->view->category = $model->fetchRow($select);
			}
		}
    }
    
    public function savestep1Action()
    {
    	$this->_helper->layout()->disableLayout();
    	
    	$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);
			$product = $model->fetchRow($select);
			$product->cat_id = $this->_request->getParam('cat_id');
			$product->save();
    	}
    	die('finish');
    }
    
    public function savestep2Action()
    {
    	$this->_helper->layout()->disableLayout();
    	$error = "";
    	$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);
			$product = $model->fetchRow($select);
			$product->title				= $this->_request->getParam('title');
			$product->description		= $this->_request->getParam('description');
			$product->status			= $this->_request->getParam('status');
			$product->start_price		= $this->_request->getParam('start_price');
			$product->expected_price	= $this->_request->getParam('expected_price');
			$product->salenow_price		= $this->_request->getParam('salenow_price');
			$product->allow_bid			= $this->_request->getParam('allow');
			$product->shipping_info		= $this->_request->getParam('shipping_info');
			$product->payment_method	= $this->_request->getParam('payment_method');
			$product->district			= $this->_request->getParam('district');
			
			if($this->_request->getParam('avaiable_time1') == 'checked'){
				$product->avaiable_time = $this->_request->getParam('avaiable_time');
			}else{
				$product->avaiable_time = $this->_request->getParam('avaiable_time1');
			}
			
			$product->save();
    	}
    	if(empty($product->title)){
    		$error .= "* Nhập tiêu đề <br/>";
    	}
    	if(empty($product->description)){
    		$error .= "* Nhập mô tả chi tiết <br/>";
    	}
    	if(empty($product->start_price)){
    		$error .= "* Nhập giá khởi điểm <br/>";
    	}
    	if(empty($product->expected_price)){
    		$error .= "* Nhập giá mong muốn <br/>";
    	}
    	if(empty($product->salenow_price)){
    		$error .= "* Nhập giá bán ngay <br/>";
    	}
    	if(empty($product->shipping_info)){
    		$error .= "* Nhập thông tin giao hàng <br/>";
    	}
    	
    	if($error){
    		die($error);
    	}
    	
    	die('finish');
    }
    
    public function savestep3Action()
    {
    	$this->_helper->layout()->disableLayout();
		$arr = $this->_request->getPost();
		$this->view->params = $arr;
		
		$targ_w = 230;
		$targ_h = 172;
		$jpeg_quality = 90;
		
		for($i = 1; $i <= 5; $i++)
		{
			$myNamespace = new Zend_Session_Namespace("crop_$i");
			if($myNamespace->fname)
			{
				$x = $this->_request->getParam("x_$i");
				$y = $this->_request->getParam("y_$i");
				$w = $this->_request->getParam("w_$i");
				$h = $this->_request->getParam("h_$i");
				if($w > 0)
				{
					$src = dirname(APPLICATION_PATH) . "/public/upload/".$myNamespace->fname;
				
					$img_r = imagecreatefromjpeg($src);
					$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
				
					imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$targ_w,$targ_h,$w,$h);
				
					$fileName = dirname(APPLICATION_PATH) . "/public/upload/thumbs/".$myNamespace->fname;
					imagejpeg($dst_r,$fileName,$jpeg_quality);
				}
			}
		}
		
		$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);
			$product = $model->fetchRow($select);
			
			$img1 = new Zend_Session_Namespace("crop_1"); $product->img1 = $img1->fname;
			$img2 = new Zend_Session_Namespace("crop_2"); $product->img2 = $img2->fname;
			$img3 = new Zend_Session_Namespace("crop_3"); $product->img3 = $img3->fname;
			$img4 = new Zend_Session_Namespace("crop_4"); $product->img4 = $img4->fname;
			$img5 = new Zend_Session_Namespace("crop_5"); $product->img5 = $img5->fname;
			$product->save();
    	}
    	if(empty($product->img1) && empty($product->img2) && empty($product->img3) && empty($product->img4) && empty($product->img5)){
    		die('* Vui lòng cung cấp hình sản phẩm');
    	}
		die('finish');
	}
	
	public function savestep4Action()
	{
		$this->_helper->layout()->disableLayout();
    	$product = null;
    	$ss = new Zend_Session_Namespace("product");
    	if(isset($ss->id))
    	{
    		$model = $this->loadModel('bid','Product');
			$select = $model->select()->where('id=?',$ss->id);
			$product = $model->fetchRow($select);
			$product->active	= 1;
			$product->created	= date("y-m-d G:i:s");
			$product->save();
			$ss = new Zend_Session_Namespace("product");
			$ss->id = "";
			
			$img1 = new Zend_Session_Namespace("crop_1"); $img1->fname = "";
			$img2 = new Zend_Session_Namespace("crop_2"); $img2->fname = "";
			$img3 = new Zend_Session_Namespace("crop_3"); $img3->fname = "";
			$img4 = new Zend_Session_Namespace("crop_4"); $img4->fname = "";
			$img5 = new Zend_Session_Namespace("crop_5"); $img5->fname = "";
    	}
		die('finish');
	}
    
    public function uploadimageAction()
    {
    	$this->_helper->layout()->disableLayout();
    	
    	$this->view->imgid = $this->_request->getParam('id');
    	
    	$target_path = dirname(APPLICATION_PATH) . "/public/upload/".$_FILES['file']['name'];
    	
    	$myNamespace = new Zend_Session_Namespace($this->view->imgid);
    	$myNamespace->fname = $_FILES['file']['name'];

		if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
		{
			$image = new My_SimpleImage();
			$image->load($target_path);
			$image->resizeToWidth(750);
			$image->save($target_path);
			$this->view->imgsrc = str_replace('index.php','',$this->view->baseUrl()).'/public/upload/'.$_FILES['file']['name'];
		}
    }
}

